package com.joven.shelltest.test;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 连续月份判断
 * @Author wzj
 * @Date 2023-08-24 10:27
 **/
public class OtherTest7 {
    public static void main(String[] args) {
        List<String> dateList = new ArrayList<>();
        dateList.add("20230716");
        dateList.add("20230616");
        dateList.add("20230516");
        dateList.add("20230416");
        dateList.add("20230316");
        dateList.add("20230216");
        dateList.add("20230116");
        dateList.add("20221216");
        dateList.add("20221116");
        dateList.add("20221016");
        dateList.add("20220916");
        dateList.add("20220816");
        dateList.add("20220716");

        Map<String, String> dateMap = new HashMap<>(16);
        dateList.stream().sorted().forEach(s -> dateMap.put(s, s.substring(4, 6)));
        int diffMonths = 0;
        for (int i = 0; i <= dateList.size(); i++) {
            int startMonth = Integer.parseInt(dateMap.get(dateList.get(i)));
            if (i + 1 == dateList.size()) {
                break;
            }
            int endMonth = Integer.parseInt(dateMap.get(dateList.get(i + 1)));
            if (startMonth - endMonth == 1) {
                diffMonths++;
            } else {
                int startYear = Integer.parseInt(dateList.get(i).substring(2, 4));
                int endYear = Integer.parseInt(dateList.get(i + 1).substring(2, 4));
                if ((startYear - endYear == 1) && Math.abs(startMonth - endMonth) == 11) {
                    diffMonths++;
                }
            }
        }
        System.out.println(diffMonths);
        if (diffMonths >= 12) {
            System.out.println("success");
        }

    }
}
